﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InSafetyAirClient.InSafetyAirService;

namespace InSafetyAirClient.GUI.Plane
{
    public partial class CreateP : UserControl
    {
        List<InSafetyAirClient.InSafetyAirService.PlaneModel> models = null;
        AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();

        public CreateP()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceSingle servS = ServiceSingle.Instance;
            foreach (InSafetyAirService.PlaneModel planeM in models)
            {
                if (planeM.planeModelName.Equals(comboBox1.Text) && textBox1.Text != "")
                {
                    InSafetyAirService.Plane plane = new InSafetyAirService.Plane();
                    plane.planeModelId = planeM.planeModelId;
                    plane.registrationNo = textBox1.Text;
                    servS.getService().AddToDatabase(plane);
                    textBox1.Text = "";
                    populateList();
                }
            }
        }

        private void CreateP_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                populateList();
            }
        }

        private void populateList()
        {
            comboBox1.Items.Clear();
            ServiceSingle servS = ServiceSingle.Instance;
            models = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.PlaneModel).Name).OfType<InSafetyAirClient.InSafetyAirService.PlaneModel>().ToList();
            foreach (InSafetyAirService.PlaneModel planeM in models)
            {
                comboBox1.Items.Add(planeM.planeModelName);
                autoComplete.Add(planeM.planeModelName);
            }
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.AutoCompleteCustomSource = autoComplete;
        }
    }
}
